package com.yeung.config;

import com.yeung.Interceptors.LoginInterceptor;
import com.yeung.component.MyLocaleResolver;
import com.yeung.servlet.MyFilter;
import com.yeung.servlet.MyListener;
import com.yeung.servlet.Myservlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Filter;
import javax.servlet.ServletContext;
import java.util.Arrays;


/**
 * 自定义一个mvc配置类
 * 实现WebMvcConfigurer 接口, 以扩展SpringMVC的功能
 * <p>
 * !!!注意:
 * 扩展类不能添加@EnableWebMvc: 因为扩展mvc配置,是添加新的配置, 原有的其他配置也生效,
 * 而添加了@EnableWebMvc: 则该类就全盘接受了该类配置,而丢弃了spring boot的默认webmvc自动配置,
 * 若自己配置不当可能会出错
 */

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    //注册三大组件: servlet, filter,Listener
    @Bean
    public ServletRegistrationBean myServlet() {
        ServletRegistrationBean<Myservlet> registrationBean = new ServletRegistrationBean<>(new Myservlet(), "/myServlet");
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean myFilter() {
        FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
        filterFilterRegistrationBean.setFilter(new MyFilter());
        filterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/hello", "/myServlet"));
        return filterFilterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean myListener() {
        ServletListenerRegistrationBean myListener = new ServletListenerRegistrationBean(new MyListener());
        return myListener;
    }


    /**
     * 添加视图控制器
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //浏览器发送/success 请求,直接跳转success页面, 而不用经过handler
        registry.addViewController("/success").setViewName("success");
        registry.addViewController("/index.html").setViewName("index");
        //为了防止重复提交,可以重定向到需要跳转的网页, 而不是直接请求
        registry.addViewController("/main.html").setViewName("dashboard");
    }

    /**
     * 像容器中添加localeResolver的bean,  实现自定义国际化功能
     *
     * @return
     */
    @Bean("localeResolver")
    public LocaleResolver registryLocale() {
        return new MyLocaleResolver();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/asserts/**")
                /* .excludePathPatterns("/asserts/css/*")        //静态资源, springboot 需要排除掉
                 .excludePathPatterns("/asserts/js/*")        //静态资源, springboot 需要排除掉
                 .excludePathPatterns("/asserts/images/*")        //静态资源, springboot 需要排除掉*/
                .excludePathPatterns("/index.html", "/index", "/", "/user/login"); //过滤掉不用拦截的请求
    }
}
